package com.example.my.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.my.LoginActivity;
import com.example.my.R;
import com.google.android.material.snackbar.Snackbar;

import utils.PreferenceUtils;


public class Tab4Fragment extends Fragment implements View.OnClickListener {


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.fragment_tab_4_layout, container,false);
        RelativeLayout info = view.findViewById(R.id.user_info);
        LinearLayout setting = view.findViewById(R.id.setting);
        TextView userName = view.findViewById(R.id.user_name);

        info.setOnClickListener(this);
        setting.setOnClickListener(this);


        if ( PreferenceUtils.getInstance(getContext()).readLoginStatus())
        {
            userName.setText(PreferenceUtils.getInstance(getContext()).readUserName());
        }
        else
        {
            userName.setText("点击登录");
        }

        return view;

    }
    @Override
    public void onClick(View view){
        switch (view.getId()){
            case R.id.setting:
                if( PreferenceUtils.getInstance(getContext()).readLoginStatus() ){
                    //TODO 已经登录了  跳转到设置页面
                }else{
                    Snackbar.make(view,"请登录", Snackbar.LENGTH_SHORT).show();
                }break;
            case R.id.user_info:
                if (PreferenceUtils.getInstance(getContext()).readLoginStatus()){
                    //todo   已经登录了   跳转到个人资料页面
                }
                else
                    {
                    Intent intent = new Intent( getContext() , LoginActivity.class);
                    getContext().startActivity(intent);
                    //todo   未登录  跳转到登录页面
                    }
                break;
        }

    }
}
